import { mockRequestClient as defHttp } from '#/api/request';
import type {
  BehaviorDefinition,
  BehaviorExecution,
  BehaviorOrchestration,
  BehaviorStatistics,
  BehaviorMonitor,
  BehaviorAlert,
} from './behavior-types';

enum Api {
  // 行为定义管理
  BEHAVIOR_DEFINITIONS = '/resource-center/behaviors/definitions',
  BEHAVIOR_DEFINITION = '/resource-center/behaviors/definitions/:id',

  // 行为执行管理
  BEHAVIOR_EXECUTIONS = '/resource-center/behaviors/executions',
  BEHAVIOR_EXECUTION = '/resource-center/behaviors/executions/:id',
  EXECUTE_BEHAVIOR = '/resource-center/behaviors/executions/:id/execute',
  CANCEL_BEHAVIOR = '/resource-center/behaviors/executions/:id/cancel',
  RETRY_BEHAVIOR = '/resource-center/behaviors/executions/:id/retry',

  // 补偿管理
  COMPENSATION_EXECUTIONS = '/resource-center/behaviors/compensations',
  COMPENSATION_EXECUTION = '/resource-center/behaviors/compensations/:id',
  TRIGGER_COMPENSATION = '/resource-center/behaviors/compensations/:id/trigger',

  // 行为编排管理
  BEHAVIOR_ORCHESTRATIONS = '/resource-center/behaviors/orchestrations',
  BEHAVIOR_ORCHESTRATION = '/resource-center/behaviors/orchestrations/:id',
  EXECUTE_ORCHESTRATION = '/resource-center/behaviors/orchestrations/:id/execute',

  // 行为监控和统计
  BEHAVIOR_STATISTICS = '/resource-center/behaviors/statistics',
  BEHAVIOR_MONITORS = '/resource-center/behaviors/monitors',
  BEHAVIOR_MONITOR = '/resource-center/behaviors/monitors/:id',
  BEHAVIOR_ALERTS = '/resource-center/behaviors/alerts',
  BEHAVIOR_ALERT = '/resource-center/behaviors/alerts/:id',

  // 资源相关行为
  RESOURCE_BEHAVIORS = '/resource-center/resources/:resourceId/behaviors',
  RESOURCE_BEHAVIOR_EXECUTIONS = '/resource-center/resources/:resourceId/behaviors/executions',
  RESOURCE_BEHAVIOR_STATISTICS = '/resource-center/resources/:resourceId/behaviors/statistics',
}

// ==================== 行为定义管理 ====================

/**
 * 获取行为定义列表
 */
export function getBehaviorDefinitions(params?: any) {
  return defHttp.get<BehaviorDefinition[]>({
    url: Api.BEHAVIOR_DEFINITIONS,
    params,
  });
}

/**
 * 获取行为定义详情
 */
export function getBehaviorDefinition(id: string) {
  return defHttp.get<BehaviorDefinition>({
    url: Api.BEHAVIOR_DEFINITION.replace(':id', id),
  });
}

/**
 * 创建行为定义
 */
export function createBehaviorDefinition(data: Partial<BehaviorDefinition>) {
  return defHttp.post<BehaviorDefinition>({
    url: Api.BEHAVIOR_DEFINITIONS,
    data,
  });
}

/**
 * 更新行为定义
 */
export function updateBehaviorDefinition(
  id: string,
  data: Partial<BehaviorDefinition>,
) {
  return defHttp.put<BehaviorDefinition>({
    url: Api.BEHAVIOR_DEFINITION.replace(':id', id),
    data,
  });
}

/**
 * 删除行为定义
 */
export function deleteBehaviorDefinition(id: string) {
  return defHttp.delete<void>({
    url: Api.BEHAVIOR_DEFINITION.replace(':id', id),
  });
}

/**
 * 启用/禁用行为定义
 */
export function toggleBehaviorDefinition(id: string, enabled: boolean) {
  return defHttp.patch<void>({
    url: Api.BEHAVIOR_DEFINITION.replace(':id', id),
    data: { enabled },
  });
}

// ==================== 行为执行管理 ====================

/**
 * 获取行为执行列表
 */
export function getBehaviorExecutions(params?: any) {
  return defHttp.get<BehaviorExecution[]>({
    url: Api.BEHAVIOR_EXECUTIONS,
    params,
  });
}

/**
 * 获取行为执行详情
 */
export function getBehaviorExecution(id: string) {
  return defHttp.get<BehaviorExecution>({
    url: Api.BEHAVIOR_EXECUTION.replace(':id', id),
  });
}

/**
 * 创建行为执行
 */
export function createBehaviorExecution(data: {
  behaviorId: string;
  resourceId: string;
  parameters?: Record<string, any>;
  priority?: string;
}) {
  return defHttp.post<BehaviorExecution>({
    url: Api.BEHAVIOR_EXECUTIONS,
    data,
  });
}

/**
 * 执行行为
 */
export function executeBehavior(id: string) {
  return defHttp.post<BehaviorExecution>({
    url: Api.EXECUTE_BEHAVIOR.replace(':id', id),
  });
}

/**
 * 取消行为执行
 */
export function cancelBehaviorExecution(id: string) {
  return defHttp.post<BehaviorExecution>({
    url: Api.CANCEL_BEHAVIOR.replace(':id', id),
  });
}

/**
 * 重试行为执行
 */
export function retryBehaviorExecution(id: string) {
  return defHttp.post<BehaviorExecution>({
    url: Api.RETRY_BEHAVIOR.replace(':id', id),
  });
}

// ==================== 补偿管理 ====================

/**
 * 获取补偿执行列表
 */
export function getCompensationExecutions(params?: any) {
  return defHttp.get<any[]>({
    url: Api.COMPENSATION_EXECUTIONS,
    params,
  });
}

/**
 * 获取补偿执行详情
 */
export function getCompensationExecution(id: string) {
  return defHttp.get<any>({
    url: Api.COMPENSATION_EXECUTION.replace(':id', id),
  });
}

/**
 * 触发补偿执行
 */
export function triggerCompensation(
  id: string,
  parameters?: Record<string, any>,
) {
  return defHttp.post<any>({
    url: Api.TRIGGER_COMPENSATION.replace(':id', id),
    data: { parameters },
  });
}

// ==================== 行为编排管理 ====================

/**
 * 获取行为编排列表
 */
export function getBehaviorOrchestrations(params?: any) {
  return defHttp.get<BehaviorOrchestration[]>({
    url: Api.BEHAVIOR_ORCHESTRATIONS,
    params,
  });
}

/**
 * 获取行为编排详情
 */
export function getBehaviorOrchestration(id: string) {
  return defHttp.get<BehaviorOrchestration>({
    url: Api.BEHAVIOR_ORCHESTRATION.replace(':id', id),
  });
}

/**
 * 创建行为编排
 */
export function createBehaviorOrchestration(
  data: Partial<BehaviorOrchestration>,
) {
  return defHttp.post<BehaviorOrchestration>({
    url: Api.BEHAVIOR_ORCHESTRATIONS,
    data,
  });
}

/**
 * 更新行为编排
 */
export function updateBehaviorOrchestration(
  id: string,
  data: Partial<BehaviorOrchestration>,
) {
  return defHttp.put<BehaviorOrchestration>({
    url: Api.BEHAVIOR_ORCHESTRATION.replace(':id', id),
    data,
  });
}

/**
 * 删除行为编排
 */
export function deleteBehaviorOrchestration(id: string) {
  return defHttp.delete<void>({
    url: Api.BEHAVIOR_ORCHESTRATION.replace(':id', id),
  });
}

/**
 * 执行行为编排
 */
export function executeOrchestration(
  id: string,
  parameters?: Record<string, any>,
) {
  return defHttp.post<BehaviorExecution>({
    url: Api.EXECUTE_ORCHESTRATION.replace(':id', id),
    data: { parameters },
  });
}

// ==================== 行为监控和统计 ====================

/**
 * 获取行为统计信息
 */
export function getBehaviorStatistics(params?: any) {
  return defHttp.get<BehaviorStatistics>({
    url: Api.BEHAVIOR_STATISTICS,
    params,
  });
}

/**
 * 获取行为监控列表
 */
export function getBehaviorMonitors(params?: any) {
  return defHttp.get<BehaviorMonitor[]>({
    url: Api.BEHAVIOR_MONITORS,
    params,
  });
}

/**
 * 获取行为监控详情
 */
export function getBehaviorMonitor(id: string) {
  return defHttp.get<BehaviorMonitor>({
    url: Api.BEHAVIOR_MONITOR.replace(':id', id),
  });
}

/**
 * 创建行为监控
 */
export function createBehaviorMonitor(data: Partial<BehaviorMonitor>) {
  return defHttp.post<BehaviorMonitor>({
    url: Api.BEHAVIOR_MONITORS,
    data,
  });
}

/**
 * 更新行为监控
 */
export function updateBehaviorMonitor(
  id: string,
  data: Partial<BehaviorMonitor>,
) {
  return defHttp.put<BehaviorMonitor>({
    url: Api.BEHAVIOR_MONITOR.replace(':id', id),
    data,
  });
}

/**
 * 删除行为监控
 */
export function deleteBehaviorMonitor(id: string) {
  return defHttp.delete<void>({
    url: Api.BEHAVIOR_MONITOR.replace(':id', id),
  });
}

/**
 * 获取行为告警列表
 */
export function getBehaviorAlerts(params?: any) {
  return defHttp.get<BehaviorAlert[]>({
    url: Api.BEHAVIOR_ALERTS,
    params,
  });
}

/**
 * 获取行为告警详情
 */
export function getBehaviorAlert(id: string) {
  return defHttp.get<BehaviorAlert>({
    url: Api.BEHAVIOR_ALERT.replace(':id', id),
  });
}

/**
 * 更新行为告警状态
 */
export function updateBehaviorAlert(id: string, data: Partial<BehaviorAlert>) {
  return defHttp.put<BehaviorAlert>({
    url: Api.BEHAVIOR_ALERT.replace(':id', id),
    data,
  });
}

/**
 * 解决行为告警
 */
export function resolveBehaviorAlert(id: string, resolvedBy: string) {
  return defHttp.patch<BehaviorAlert>({
    url: Api.BEHAVIOR_ALERT.replace(':id', id),
    data: { resolved: true, resolvedBy, resolvedAt: new Date().toISOString() },
  });
}

// ==================== 资源相关行为 ====================

/**
 * 获取资源的可用行为列表
 */
export function getResourceBehaviors(resourceId: string) {
  return defHttp.get<BehaviorDefinition[]>({
    url: Api.RESOURCE_BEHAVIORS.replace(':resourceId', resourceId),
  });
}

/**
 * 获取资源的行为执行历史
 */
export function getResourceBehaviorExecutions(
  resourceId: string,
  params?: any,
) {
  return defHttp.get<BehaviorExecution[]>({
    url: Api.RESOURCE_BEHAVIOR_EXECUTIONS.replace(':resourceId', resourceId),
    params,
  });
}

/**
 * 获取资源的行为统计信息
 */
export function getResourceBehaviorStatistics(resourceId: string) {
  return defHttp.get<BehaviorStatistics>({
    url: Api.RESOURCE_BEHAVIOR_STATISTICS.replace(':resourceId', resourceId),
  });
}
